package com.yyjj.yyjj.home.service.impl;

import com.yyjj.yyjj.activity.client.ActivityFeignClient;
import com.yyjj.yyjj.client.product.ProductFeignClient;
import com.yyjj.yyjj.client.search.SearchFeignClient;
import com.yyjj.yyjj.home.service.ItemService;
import com.yyjj.yyjj.model.product.SkuInfo;
import com.yyjj.yyjj.vo.product.SkuInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    
    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private ActivityFeignClient activityFeignClient;

    @Resource
    private SearchFeignClient searchFeignClient;

    //商品详情
    @Override
    public Map<String, Object> item(Long skuId, Long userId) {
        Map<String,Object> result=new HashMap<>();

        //根据skuid查询
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //远程调用获取sku数据
            SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVo(skuId);
            result.put("skuInfoVo", skuInfoVo);
            return skuInfoVo;
        },threadPoolExecutor);

        //sku对应的优惠券信息
        CompletableFuture<Void> activityCompletableFuture = CompletableFuture.runAsync(() -> {
            Map<String, Object> activityMap = activityFeignClient.findActivityAndCoupon(skuId,userId);
            result.putAll(activityMap);
        },threadPoolExecutor);

        //更新商品热度
        CompletableFuture<Void> hotCompletableFuture=CompletableFuture.runAsync(()->{
            //远程调用更新热度
            searchFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //任务的组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                activityCompletableFuture,
                hotCompletableFuture
        ).join();
        return  result;
    }
}
